<?PHP

require_once("util.php"); 
require_once("sql.php"); 
require_once("document.php"); 
require_once("statutory.php"); 

class ManageStatutoryDocumentsMapping {

public function getStatutoryMappedToDocument($documentId){

	$util=new DBUtil();
		$conn=$util->getConnection();
		$sql = SQLQuery::DOCUMENTSTATUTORYMAPPING_FIND_BYID;
		$stmt = $conn->prepare( $sql);
		$stmt->bind_param('d',$documentId);
		$status=$stmt->execute();
		$result = $stmt->get_result();
		$mappings =array();
		if ($result->num_rows > 0) {
			while($row = $result->fetch_array()) {
				$statutory=new Statutory();
				$statutory->setId($row["STATUTORYID"]);
				$statutory->setName($row["STATUTORYNAME"]);
				//$statutory->setDescription($row["VALIDFOR"]);
				array_push($mappings,$statutory);
			}
		}
		$conn->close();
		return $mappings;

}


public function getStatutoryNotMappedToDocument($documentId){

	$util=new DBUtil();
		$conn=$util->getConnection();
		$sql = SQLQuery::UNMAPPEDSTATUTORYFORDOCUMENT;
		$stmt = $conn->prepare( $sql);
		$stmt->bind_param('d',$documentId);
		$status=$stmt->execute();
		$result = $stmt->get_result();
		$unmappedstatutories =array();
		if ($result->num_rows > 0) {
			while($row = $result->fetch_array()) {
				$statutory=new Statutory();
				$statutory->setId($row["ID"]);
				$statutory->setName($row["NAME"]);
				//$statutory->setDescription($row["VALIDFOR"]);
				array_push($unmappedstatutories,$statutory);
			}
		}
		$conn->close();
		return $unmappedstatutories;

}

public function updateStatutoryDocumentMapping($documentId,$statutoryIds){

		$util=new DBUtil();
		$conn=$util->getConnection();
		$conn->autocommit(false);
		
		//Delete Document to Statutories mapping
			$sql = SQLQuery::DOCUMENTSTATUTORYMAPPING_DELETE;
			$stmt = $conn->prepare( $sql);
			$stmt->bind_param('d',$documentId);
			$status=$stmt->execute();
			$stmt->close();
		
		if(isset($statutoryIds)){
			//Add Role to Operation mapping
			foreach($statutoryIds as $statutoryId){
				$sql = SQLQuery::DOCUMENTSTATUTORYMAPPING_ADD;
				$stmt = $conn->prepare( $sql);
				$stmt->bind_param('dd',$documentId,$statutoryId);
				$status =$stmt->execute();
			}
			$stmt->close();
		}
		$conn->commit();
		$conn->autocommit(true);
		$conn->close();
		return $status;
}

}


?>